package com.cn.demo.service.impl;

import com.cn.demo.entity.Organization;
import com.cn.demo.entity.Student;
import com.cn.demo.entity.WaitJoinCurriculum;
import com.cn.demo.mapper.CurriculumMapper;
import com.cn.demo.mapper.WaitJoinCurriculumMapper;
import com.cn.demo.service.PageInfoUtilServer;
import com.cn.demo.service.WaitJoinCurriculumService;
import com.cn.demo.utils.Result;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class WaitJoinCurriculumServiceImpl implements WaitJoinCurriculumService {
    @Autowired
    private WaitJoinCurriculumMapper waitJoinCurriculumMapper;

    @Autowired
    private CurriculumMapper curriculumMapper;

    @Autowired
    private PageInfoUtilServer pageInfoUtilServer;

    @Override
    public Result joinCurriculum(String studentId, String curriculumId) {
        WaitJoinCurriculum waitJoinCurriculum = new WaitJoinCurriculum(curriculumId,studentId);
        if (curriculumMapper.isJoin(waitJoinCurriculum)>0){
            return Result.error("您你加入该课程");
        }
        if (waitJoinCurriculumMapper.isInfo(waitJoinCurriculum)!=null){
            return Result.error("您已经发送请求");
        }
        if (waitJoinCurriculumMapper.insert(waitJoinCurriculum) > 0)  {
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result getRequestStudent(Integer page, Integer limit, String curriculumId) {
        PageHelper.startPage(page, limit);
        List<Student> studentList = waitJoinCurriculumMapper.getRequestStudent(curriculumId);
        Map map = pageInfoUtilServer.currency(studentList);
        return Result.success(map);
    }
}